from flask import Blueprint,render_template,g,redirect,request
from app import db
from app.models import Product,User,Acctproduct,Updateturn,Updatejour,Updatepath
from app.method.web_methods import jar_version
import os
import time

bop_jar_version_page = Blueprint('bop_jar_version_page',__name__,template_folder='templates')
@bop_jar_version_page.route('/bop-jar-version/<ip>',methods = ['GET','POST'])
def bop_jar_version(ip):
    if g.user is not None and g.user.is_authenticated:
        hs_current_id = str(g.user).replace("'", "")
        hs_current_user = db.session.query(User.username).filter_by(id=hs_current_id).first()[0]
        mes_lists = db.session.query(Updatejour.output,Updatejour.update_date,Updatejour.update_time,Updatejour.message,Updatejour.id,Updatejour.log_path).filter_by(read_status = 1,up_persion=hs_current_user).all()
        mes_len = len(mes_lists)

        path = db.session.query(Updatepath.to_path).filter_by(server_ip=ip, path_type=2).all()[0]
        data = db.session.query(Product.server_port,Product.server_user,Product.server_password).filter_by(server_address = ip).all()[0]
        web_list = jar_version(ip=ip,port=data[0],username=data[1],password=data[2],path=path[0],war='bop-web')
        trade_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-trade')
        acct_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-acct')
        ext_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-ext')
        flow_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-flow')
        witness_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-witness')
        flow_view_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-flow-view')
        svr_list = jar_version(ip=ip, port=data[0], username=data[1], password=data[2], path=path[0], war='bop-svr')

    else:
        return redirect('/login')
    return render_template('hs-bop-jar.html',hs_current_user=hs_current_user,mes_lists=mes_lists,mes_len=mes_len,web_list=web_list,trade_list=trade_list,acct_list=acct_list,ext_list=ext_list,flow_list=flow_list,witness_list=witness_list,flow_view_list=flow_view_list,svr_list=svr_list)